package cn.org.intelli.zffserver.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Component
public class HttpClient {
    /*  12 */   private static final Logger log = LoggerFactory.getLogger(cn.org.intelli.zffserver.util.HttpClient.class);


    public String get(String restApiPath, String ip, String port, Map<String, String> headers) throws Exception {
        /*  23 */
        return get(restApiPath, ip, port, headers, Long.valueOf(3000L));
    }

    public String get(String restApiPath, String ip, String port, Map<String, String> headers, Long time) throws Exception {
        String url = "http://" + ip + ":" + port + "/" + restApiPath;
        log.info("@HttpClient: Get url is {}", url);

        /*  30 */
        OkHttpClient okHttpClient = (new OkHttpClient()).newBuilder().connectTimeout(time.longValue(), TimeUnit.MILLISECONDS).build();

        /*  32 */
        Request.Builder builder = new Request.Builder();
        /*  33 */
        builder.url(url);
        /*  34 */
        if (headers != null && headers.size() > 0) {
            /*  35 */
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                /*  36 */
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        /*  39 */
        Request request = builder.build();

        /*  41 */
        Response response = okHttpClient.newCall(request).execute();
        /*  42 */
        if (!response.isSuccessful()) {
            /*  43 */
            throw new Exception("okhttp client failture for syncGet!");
        }
        /*  45 */
        return response.body().string();
    }


    public String post(String restApiPath, JSONObject requestBody, String ip, String port, Map<String, String> headers) throws Exception {
        /*  58 */
        return post(restApiPath, requestBody, ip, port, headers, null);
    }

    public String post(String restApiPath, JSONObject requestBody, String ip, String port, Map<String, String> headers, Long time) throws Exception {
        /*  62 */
        String url = "http://" + ip + ":" + port + "/" + restApiPath;
        /*  63 */
        log.info("@HttpClient: Post url is {}", url);
        /*  64 */
        MediaType mediaType = MediaType.parse("application/json");

        /*  66 */
        Request.Builder builder = new Request.Builder();
        /*  67 */
        builder.url(url);

        /*  69 */
        builder.post(RequestBody.create(mediaType, requestBody.toJSONString()));
        /*  70 */
        if (headers != null && headers.size() > 0) {
            /*  71 */
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                /*  72 */
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        /*  75 */
        Request request = builder.build();

        /*  77 */
        OkHttpClient okHttpClient = null;
        /*  78 */
        if (time == null) {
            /*  79 */
            okHttpClient = new OkHttpClient();
        } else {
            /*  81 */
            okHttpClient = (new OkHttpClient()).newBuilder().connectTimeout(time.longValue(), TimeUnit.MILLISECONDS).build();
        }
        /*  83 */
        Response response = okHttpClient.newCall(request).execute();
        /*  84 */
        if (!response.isSuccessful()) {
            /*  85 */
            log.error("okhttp client failture for post!   " + response.body().string());
            /*  86 */
            throw new Exception("okhttp client failture for post!");
        }
        /*  88 */
        return response.body().string();
    }


    public byte[] getbyte(String restApiPath, String ip, String port, Map<String, String> headers) throws Exception {
        /*  99 */
        return getbyte(restApiPath, ip, port, headers, Long.valueOf(3000L));
    }

    public byte[] getbyte(String restApiPath, String ip, String port, Map<String, String> headers, Long time) throws Exception {
        /* 103 */
        String url = "http://" + ip + ":" + port + "/" + restApiPath;
        /* 104 */
        log.info("@HttpClient: Get url is {}", url);

        /* 106 */
        OkHttpClient okHttpClient = (new OkHttpClient()).newBuilder().connectTimeout(time.longValue(), TimeUnit.MILLISECONDS).build();

        /* 108 */
        Request.Builder builder = new Request.Builder();
        /* 109 */
        builder.url(url);
        /* 110 */
        if (headers != null && headers.size() > 0) {
            /* 111 */
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                /* 112 */
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        /* 115 */
        Request request = builder.build();

        /* 117 */
        Response response = okHttpClient.newCall(request).execute();
        /* 118 */
        if (!response.isSuccessful()) {
            /* 119 */
            throw new Exception("okhttp client failture for syncGet! 报错内" + response.body().string());
        }
        /* 121 */
        return response.body().bytes();
    }


    public byte[] postbyte(String restApiPath, JSONObject requestBody, String ip, String port, Map<String, String> headers) throws Exception {
        /* 133 */
        return postbyte(restApiPath, requestBody, ip, port, headers, null);
    }

    public byte[] postbyte(String restApiPath, JSONObject requestBody, String ip, String port, Map<String, String> headers, Long time) throws Exception {
        /* 137 */
        String url = "http://" + ip + ":" + port + "/" + restApiPath;
        /* 138 */
        log.info("@HttpClient: Post url is {}", url);
        /* 139 */
        MediaType mediaType = MediaType.parse("application/json");

        /* 141 */
        Request.Builder builder = new Request.Builder();
        /* 142 */
        builder.url(url);

        /* 144 */
        builder.post(RequestBody.create(mediaType, requestBody.toJSONString()));
        /* 145 */
        if (headers != null && headers.size() > 0) {
            /* 146 */
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                /* 147 */
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        /* 150 */
        Request request = builder.build();

        /* 152 */
        OkHttpClient okHttpClient = null;
        /* 153 */
        if (time == null) {
            /* 154 */
            okHttpClient = new OkHttpClient();
        } else {
            /* 156 */
            okHttpClient = (new OkHttpClient()).newBuilder().connectTimeout(time.longValue(), TimeUnit.MILLISECONDS).build();
        }
        /* 158 */
        Response response = okHttpClient.newCall(request).execute();
        /* 159 */
        if (!response.isSuccessful()) {
            /* 160 */
            throw new Exception("okhttp client failture for post!");
        }
        /* 162 */
        return response.body().bytes();
    }
}